/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.tms.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.tms.entity.Notnode;
import com.jeeplus.modules.tms.service.NotnodeService;

/**
 * 节点跟踪Controller
 * @author 李超辉
 * @version 2017-12-07
 */
@Controller
@RequestMapping(value = "${adminPath}/tms/notnode")
public class NotnodeController extends BaseController {

	@Autowired
	private NotnodeService notnodeService;
	
	@ModelAttribute
	public Notnode get(@RequestParam(required=false) String id) {
		Notnode entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = notnodeService.get(id);
		}
		if (entity == null){
			entity = new Notnode();
		}
		return entity;
	}
	
	/**
	 * 节点跟踪列表页面
	 */
	@RequiresPermissions("tms:notnode:list")
	@RequestMapping(value = {"list", ""})
	public String list() {
		return "modules/tms/notnodeList";
	}
	
		/**
	 * 节点跟踪列表数据
	 */
	@ResponseBody
	@RequiresPermissions("tms:notnode:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(Notnode notnode, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Notnode> page = notnodeService.findPage(new Page<Notnode>(request, response), notnode); 
		return getBootstrapData(page);
	}

	/**
	 * 查看，增加，编辑节点跟踪表单页面
	 */
	@RequiresPermissions(value={"tms:notnode:view","tms:notnode:add","tms:notnode:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(Notnode notnode, Model model) {
		model.addAttribute("notnode", notnode);
		return "modules/tms/notnodeForm";
	}

//	/**
//	 * 保存节点跟踪
//	 */
//	@ResponseBody
//	@RequiresPermissions(value={"tms:notnode:add","tms:notnode:edit"},logical=Logical.OR)
//	@RequestMapping(value = "save")
//	public AjaxJson save(Notnode notnode, Model model, RedirectAttributes redirectAttributes) throws Exception{
//		AjaxJson j = new AjaxJson();
//		if (!beanValidator(model, notnode)){
//			j.setSuccess(false);
//			j.setMsg("非法参数！");
//			return j;
//		}
//		notnodeService.save(notnode);//新建或者编辑保存
//		j.setSuccess(true);
//		j.setMsg("保存节点跟踪成功");
//		return j;
//	}
	/**
	 * 保存节点跟踪
	 */
	@ResponseBody
	@RequiresPermissions(value={"tms:notnode:add","tms:notnode:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(Notnode notnode, String person,String trackTime,String trackType,String trackState,String trackLocation,String remarks ,String bill) throws Exception{
        notnode.setBill(bill);
        notnode.setPerson(person);
        notnode.setTrackLocation(trackLocation);
        notnode.setTrackState(trackState);
        notnode.setTrackTime(trackTime);
        notnode.setTrackType(trackType);
        notnode.setRemarks(remarks);
		notnodeService.save(notnode);//新建或者编辑保存

		return notnode.getId();
	}
	/**
	 * 删除节点跟踪
	 */
	@ResponseBody
	@RequiresPermissions("tms:notnode:del")
	@RequestMapping(value = "delete")
	public AjaxJson delete(Notnode notnode, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		notnodeService.delete(notnode);
		j.setMsg("删除节点跟踪成功");
		return j;
	}
	
	/**
	 * 批量删除节点跟踪
	 */
	@ResponseBody
	@RequiresPermissions("tms:notnode:del")
	@RequestMapping(value = "deleteAll")
	public AjaxJson deleteAll(String ids, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		String idArray[] =ids.split(",");
		for(String id : idArray){
			notnodeService.delete(notnodeService.get(id));
		}
		j.setMsg("删除节点跟踪成功");
		return j;
	}
	
	/**
	 * 导出excel文件
	 */
	@ResponseBody
	@RequiresPermissions("tms:notnode:export")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public AjaxJson exportFile(Notnode notnode, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		try {
            String fileName = "节点跟踪"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<Notnode> page = notnodeService.findPage(new Page<Notnode>(request, response, -1), notnode);
    		new ExportExcel("节点跟踪", Notnode.class).setDataList(page.getList()).write(response, fileName).dispose();
    		j.setSuccess(true);
    		j.setMsg("导出成功！");
    		return j;
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("导出节点跟踪记录失败！失败信息："+e.getMessage());
		}
			return j;
    }

	/**
	 * 导入Excel数据

	 */
	@RequiresPermissions("tms:notnode:import")
    @RequestMapping(value = "import", method=RequestMethod.POST)
    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<Notnode> list = ei.getDataList(Notnode.class);
			for (Notnode notnode : list){
				try{
					notnodeService.save(notnode);
					successNum++;
				}catch(ConstraintViolationException ex){
					failureNum++;
				}catch (Exception ex) {
					failureNum++;
				}
			}
			if (failureNum>0){
				failureMsg.insert(0, "，失败 "+failureNum+" 条节点跟踪记录。");
			}
			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条节点跟踪记录"+failureMsg);
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入节点跟踪失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/tms/notnode/?repage";
    }
	
	/**
	 * 下载导入节点跟踪数据模板
	 */
	@RequiresPermissions("tms:notnode:import")
    @RequestMapping(value = "import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "节点跟踪数据导入模板.xlsx";
    		List<Notnode> list = Lists.newArrayList(); 
    		new ExportExcel("节点跟踪数据", Notnode.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/tms/notnode/?repage";
    }

}